/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.common.web.filter.SSLFilter
   Module Description   :

   Date Created      : 2010/5/10
   Original Author   : jeffma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.common.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.yaodian100.core.common.utils.ServerValue;

/**
 * @author jeffma
 * 
 */
public class SSLFilter implements Filter {

	String[] enablePattern = null;
	String[] disablePattern = null;

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#destroy()
	 */
	@Override
	public void destroy() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse,
	 * javax.servlet.FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		try {
			if (!request.isSecure() && enablePattern != null && isMatchPattern(httpRequest, enablePattern)) {
				HttpServletResponse httpResponse = (HttpServletResponse) response;

				String uri = ServerValue.getFrontHostnameSsl() + httpRequest.getRequestURI();
				if (StringUtils.isNotBlank(httpRequest.getQueryString())) {
					uri += "?" + httpRequest.getQueryString();
				}
				httpResponse.sendRedirect(uri);
			} else {
				if (request.isSecure() && isMatchPattern(httpRequest, disablePattern)) {
					HttpServletResponse httpResponse = (HttpServletResponse) response;

					String uri = ServerValue.getFrontHostname() + httpRequest.getRequestURI();
					if (StringUtils.isNotBlank(httpRequest.getQueryString())) {
						uri += "?" + httpRequest.getQueryString();
					}
					httpResponse.sendRedirect(uri);
				} else {
					chain.doFilter(request, response);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		String pattern = filterConfig.getInitParameter("sslpattern");
		if (StringUtils.isNotBlank(pattern)) {
			enablePattern = StringUtils.split(pattern, ";");
		}

		pattern = filterConfig.getInitParameter("nosslpattern");
		if (StringUtils.isNotBlank(pattern)) {
			disablePattern = StringUtils.split(pattern, ";");
		}
	}

	private boolean isMatchPattern(HttpServletRequest httpRequest, String[] pattern) {
		boolean result = false;
		String url = StringUtils.remove(httpRequest.getRequestURI(), httpRequest.getContextPath());

		if (pattern != null && StringUtils.isNotBlank(url)) {
			for (String u : pattern) {
				if (url.startsWith(u)) {
					result = true;
					break;
				}
			}
		}
		return result;
	}
}
